

#include <cegfx/cerendermesh.h>


ceRenderMesh::ceRenderMesh ()
	: vertexBuffer (0)
	, numVertices(0)
	, indexBuffer (0)
	, numTriangles (0)
	, vertexDeclaration(0)
	, stride (0)
{
}

ceRenderMesh::~ceRenderMesh ()
{
	if (vertexBuffer)
	{
		vertexBuffer->Release ();
	}
	if (indexBuffer)
	{
		indexBuffer->Release ();
	}
	if (vertexDeclaration)
	{
		vertexDeclaration->Release ();
	}
}

IQF_IMPLEMENTATION_BEGIN (ceRenderMesh);
IQF_IMPLEMENTATION_INTERFACE(iGeometry);
IQF_IMPLEMENTATION_END ();


void ceRenderMesh::SetVertices (LPDIRECT3DVERTEXBUFFER9 vertexBuffer, unsigned numVertices)
{
	if (vertexBuffer)
	{
		vertexBuffer->AddRef ();
	}
	if (this->vertexBuffer)
	{
		this->vertexBuffer->Release ();
	}
	this->vertexBuffer = vertexBuffer;
	this->numVertices = numVertices;
}

LPDIRECT3DVERTEXBUFFER9 ceRenderMesh::GetVertexBuffer ()
{
	return vertexBuffer;
}

const LPDIRECT3DVERTEXBUFFER9 ceRenderMesh::GetVertexBuffer () const
{
	return vertexBuffer;
}

unsigned ceRenderMesh::GetNumVertices () const
{
	return numVertices;
}

void ceRenderMesh::SetVertexDeclaration (LPDIRECT3DVERTEXDECLARATION9 declaration, unsigned stride)
{
	if (declaration)
	{
		declaration->AddRef ();
	}
	if (vertexDeclaration)
	{
		vertexDeclaration->Release ();
	}

	vertexDeclaration = declaration;
	this->stride = stride;
}

LPDIRECT3DVERTEXDECLARATION9 ceRenderMesh::GetVertexDeclaration ()
{
	return vertexDeclaration;
}

const LPDIRECT3DVERTEXDECLARATION9 ceRenderMesh::GetVertexDeclaration () const
{
	return vertexDeclaration;
}

unsigned ceRenderMesh::GetStride () const
{
	return stride;
}

void ceRenderMesh::SetIndices (LPDIRECT3DINDEXBUFFER9 indexBuffer, unsigned numTriangles)
{
	if (indexBuffer)
	{
		indexBuffer->AddRef ();
	}
	if (this->indexBuffer)
	{
		this->indexBuffer->Release ();
	}
	this->indexBuffer = indexBuffer;
	this->numTriangles = numTriangles;
}

LPDIRECT3DINDEXBUFFER9 ceRenderMesh::GetIndexBuffer ()
{
	return indexBuffer;
}

const LPDIRECT3DINDEXBUFFER9 ceRenderMesh::GetIndexBuffer () const
{
	return indexBuffer;
}

unsigned ceRenderMesh::GetNumberOfTriangles () const
{
	return numTriangles;
}



void ceRenderMesh::Render (const ceRenderEnv& env)
{
	LPDIRECT3DDEVICE9 device = env.device;
	device->SetVertexDeclaration (vertexDeclaration);
	device->SetStreamSource (0, vertexBuffer, 0, stride);
	device->SetIndices (indexBuffer);
	device->DrawIndexedPrimitive (D3DPT_TRIANGLELIST, 0, 0, numVertices, 0, numTriangles);
}


void ceRenderMesh::Update (const ceUpdateEnv& env)
{
}


ceBoundingBox& ceRenderMesh::GetBoundingBox ()
{
	return boundingBox;
}

const ceBoundingBox& ceRenderMesh::GetBoundingBox () const
{
	return boundingBox;
}

